package demo2;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @description:实现TCP的网络编程 客户端发送文件给服务器，服务器将文件保存在本地
 * @author:Mr.Cat
 * @create:2021-11-14 21:18
 **/
public class TCPTest2 {

    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            //指定IP
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            //指定端口号
            socket = new Socket(inet, 8811);
            //获取一个输出流用于输出数据
            os = socket.getOutputStream();
            //获取一个文件输出流，输出指定的文件
            fis = new FileInputStream(new File("XG.png"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //服务端
    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            //设置服务端的端口号
            ss = new ServerSocket(8811);
            //调用accept（）表示接受客户端的信息
            socket = ss.accept();
            //定义一个输入流，输入客户端的数据
            is = socket.getInputStream();
            //定义一个文件输出流将数据输出到指定的文件
            fos = new FileOutputStream(new File("C:\\Users\\猫先生\\Desktop\\XGMM.png"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

